// composables/useAnnouncement.ts

import { ref, reactive, onMounted } from 'vue';
import type { Announcement } from "@/interface/announcementData";
import axios from 'axios';
import { ElMessage } from 'element-plus';
export function useAnnouncement() {

  //数据定义==============================================================
  const loading = ref(false);
  const noticeList = ref<Announcement[]>([]);
  const dialogVisible = ref(false);

  const form = reactive<Partial<Announcement>>({
    id: null,
    title: '',
    detaile: '',
    status: 'active',
    userIdentifier: null,
    createTime: new Date(),
    publishTime: new Date(),
  });

  // 后端请求==============================================
  async function REQ_getAllAnnouncement() {
    await axios.get("/api/announcement/allAnnouncement")
      .then(response => {
        console.log("公告数据：", response.data.data);
        noticeList.value = response.data.data;
      })
      .catch(error => {
        ElMessage.warning("获取公告，请求失败")
      })
  }


  //=界面函数===================================================================== 
  // 加载公告
  const loadNotices = async () => {
    loading.value = true;
    await REQ_getAllAnnouncement();
    loading.value = false;
  };

  onMounted(loadNotices);

  return {
    loading,
    noticeList,
    dialogVisible,
    form,
  };
}
